﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Founder.PKURG.CRM
{
    public class ConfigInfo
    {
        public string ISIFD { get; set; }

        /// <summary>
        ///  CRM IFD  OrgServiceUri
        /// </summary>
        public string OrgServiceUri { get; set; }

        /// <summary>
        /// CRM IFD  user name
        /// </summary>
        public string UserName { get; set; }


        /// <summary>
        /// CRM IFD  user password
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// CRM IFD  dommin name
        /// </summary>
        public string Domain { get; set; }


        public static ConfigInfo GetConfigInfo(string connectionString)
        {
            ConfigInfo info = new ConfigInfo();
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentNullException("connectionString", "请设置连接字符串");
            }

            MatchCollection matchs = Regex.Matches(connectionString, ";+(?<key>(.*?))=(?<value>(.*?));+", RegexOptions.IgnoreCase);

            foreach (System.Reflection.PropertyInfo propInfo in typeof(ConfigInfo).GetProperties())
            {
                string value = "";
                if (!TryGetPropName(propInfo.Name,matchs,out value))
                {
                    throw new ArgumentException("连接字符串不完整");
                }

                propInfo.SetValue(info, value,null);
            }

            return info;
        }

        public static ConfigInfo GetConfigInfoV2(string connectionString)
        {
            ConfigInfo info = new ConfigInfo();
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentNullException("connectionString", "请设置连接字符串");
            }

            string[] matchs = Regex.Split(connectionString, ";", RegexOptions.IgnoreCase);
            Dictionary<string, string> pairs = new Dictionary<string, string>();
            foreach (string item in matchs)
            {
                Match match = Regex.Match(item,"(?<key>(.*?))=(?<value>(.*))");
                if (match.Success)
                {
                    pairs.Add(match.Groups["key"].Value.ToLower(),match.Groups["value"].Value);
                }
            }
            foreach (System.Reflection.PropertyInfo propInfo in typeof(ConfigInfo).GetProperties())
            {
                if (!pairs.ContainsKey(propInfo.Name.ToLower()))
                {
                    throw new ArgumentException("连接字符串不完整");
                }

                propInfo.SetValue(info, pairs[propInfo.Name.ToLower()], null);
            }

            return info;
        }

        private static bool TryGetPropName(string name, MatchCollection matchs,out string value)
        {
            foreach (Match item in matchs)
            {
                if (name.ToLower() == item.Groups["key"].Value.ToLower())
                {
                    value = item.Groups["value"].Value;
                    return true;
                }
            }
            value = "";
            return false;
        }
    }
}
